Page({
  data: {
    backendUrl: 'http://127.0.0.1:88/api/user/',
    course: null,
    suitableList: [],
    noticeList: [],
    formattedStartTime: '',
    formattedEndTime: ''
  },

  onLoad(options) {
    const courseId = options.id;
    this.loadCourseDetail(courseId);
  },

  // 格式化时间 (去掉秒数)
  formatTime(timeString) {
    return timeString.substring(0, 5);
  },

  // 加载课程详情
  loadCourseDetail(id) {
    wx.showLoading({
      title: '加载中...',
      mask: true
    });

    wx.request({
      url: this.data.backendUrl + 'course_booking.php',
      method: 'GET',
      data: { 
        action: 'get_course_detail',
        id: id
      },
      success: (res) => {
        if (res.data.status === 'success') {
          const course = res.data.course;
          // 将适合人群和课程须知字符串转换为数组
          const suitableList = course.suitable_for ? course.suitable_for.split('\n') : [];
          const noticeList = course.notice ? course.notice.split('\n') : [];
          
          // 格式化时间
          const formattedStartTime = this.formatTime(course.start_time);
          const formattedEndTime = this.formatTime(course.end_time);
          
          this.setData({
            course: course,
            suitableList: suitableList,
            noticeList: noticeList,
            formattedStartTime,
            formattedEndTime
          });
        } else {
          wx.showToast({
            title: res.data.message || '加载失败',
            icon: 'none'
          });
        }
      },
      complete: () => {
        wx.hideLoading();
      }
    });
  },

  // 提交预约
  submitBooking() {
    // 检查是否登录
    const userInfo = wx.getStorageSync('userInfo');
    if (!userInfo) {
      wx.showToast({
        title: '请先登录',
        icon: 'none',
        duration: 1500
      });
      setTimeout(() => {
        wx.navigateTo({
          url: '/pages/login/login'
        });
      }, 1500);
      return;
    }

    // 检查是否可预约
    if (this.data.course.booked_count >= this.data.course.capacity) {
      wx.showToast({
        title: '该课程已约满',
        icon: 'none'
      });
      return;
    }

    wx.showLoading({
      title: '预约中...',
      mask: true
    });

    // 提交预约
    wx.request({
      url: this.data.backendUrl + 'course_booking.php',
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        action: 'create_booking',
        user_id: userInfo.id,
        course_id: this.data.course.id,
        booking_date: this.data.course.course_date,
        time_slot: this.data.course.start_time + '-' + this.data.course.end_time
      },
      success: (res) => {
        wx.hideLoading();
        
        if (res.data.status === 'success') {
          wx.showToast({
            title: res.data.message,
            icon: 'success'
          });
          setTimeout(() => {
            wx.navigateTo({
              url: '/pages/my-bookings/my-bookings'
            });
          }, 2000);
        } else {
          wx.showToast({
            title: res.data.message || '预约失败',
            icon: 'none'
          });
        }
      },
      fail: () => {
        wx.hideLoading();
        wx.showToast({
          title: '网络错误',
          icon: 'none'
        });
      }
    });
  }
}); 